<?php
class ModelMessageOrder extends Model {
	
	public function addOrder($data){
		$this->db->query("insert into `".DB_PREFIX."message_order` set card_no='".$data['card_no']."',bussness_type_id='".(int)$data['bussness_type_id']."'");
	}
	
	public function editOrder($order_id, $data) {
		$this->db->query("update  `".DB_PREFIX."message_order`  set card_no='".$data['card_no']."',bussness_type_id='".(int)$data['bussness_type_id']."',summary='".$data['summary']."' where order_id='".(int)$order_id."'");
	}

	
	public function deleteOrder($order_id){
		$this->db->query('delete from '.DB_PREFIX."message_order where order_id='".(int)$order_id."'");
	}
	
	private function filterSql($data=array()){
		$sql ="";
		if(isset($data['filter_name'])&&!empty($data['filter_name'])){
			$sql .=" and name like '%".$data['filter_name']."%'";
		}
		return $sql;
	}
	
	public function getOrders($data=array()){
		$sql = "select * from `".DB_PREFIX."message_order` o left join ".DB_PREFIX."config_bussness_type cbt on(o.bussness_type_id=cbt.bussness_type_id) where 1=1";
		$sql .=$this->filterSql($data);	
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getOrder($order_id){
		$query = $this->db->query("SELECT *  FROM `" . DB_PREFIX . "message_order` where order_id='".(int)$order_id."'");
		return $query->row;
	}
	
	public function getTotalOrders(){
		$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "message_order`");
		return $query->row['total'];
	}
	
	
	public function getOrderInfos($order_id){
		$sql = "select * from ".DB_PREFIX."message_user_upload_info  where order_id='".(int)$order_id."'";
		return $this->db->query($sql)->rows;
	}
	
	public function getUploadInfo($user_upload_info_id){
		$sql = "select * from ".DB_PREFIX."message_user_upload_info  where user_upload_info_id='".(int)$user_upload_info_id."'";
		return $this->db->query($sql)->row;
	}
	
}
?>